package com.heima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.reggie.common.R;
import com.heima.reggie.domain.AddressBook;
import com.heima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.List;


@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook , HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        log.info("adressbook==========={}",addressBook);
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook :: getUserId , userId);
        List<AddressBook> list = addressBookService.list(addressBookLambdaQueryWrapper);
        return R.success(list);
    }

    @PutMapping("/default")
    public R<String> setDefault(HttpSession session , @RequestBody AddressBook addressBook){
        Long userId = (Long) session.getAttribute("user");
        Long id = addressBook.getId();
        LambdaUpdateWrapper<AddressBook> addressBookLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<AddressBook> updateWrapper1 = addressBookLambdaUpdateWrapper.set(AddressBook::getIsDefault, 0)
                .eq(AddressBook::getUserId, userId);
        LambdaUpdateWrapper<AddressBook> addressBookLambdaUpdateWrapper1 = new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<AddressBook> updateWrapper2 = addressBookLambdaUpdateWrapper1.set(AddressBook::getIsDefault, 1)
                .eq(AddressBook::getId, id);
        addressBookService.update(updateWrapper1);
        addressBookService.update(updateWrapper2);
        return R.success("修改成功");
    }

    //回显
    @GetMapping("/{id}")
    public R<AddressBook> back(@PathVariable Long id){
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook :: getId , id);
        AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);
        return R.success(addressBook);

    }

    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");

    }

    @DeleteMapping
    public R<String> delete(Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }

    @GetMapping("/default")
    public R<AddressBook> getDefault(HttpSession session){
        Long userId = (Long)session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook :: getIsDefault , 1).eq(AddressBook :: getUserId , userId);
        AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);
        return R.success(addressBook);
    }
}
